/**

* <p>Title: OrderController.java</p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2021</p>

* <p>Company: www.chinasofti.com</p>

* @author knowno

* @date 2021年3月9日

* @version 1.0

*/
package com.etc.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Items;
import com.etc.entity.Order;
import com.etc.entity.ShopcObj;
import com.etc.entity.ShopcObjs;
import com.etc.entity.User;
import com.etc.util.AjaxResponse;
import com.etc.util.OrderIdUtil;
import com.etc.zmall.service.OrdersService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * <p>
 * Title: OrderController
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * @author knowno
 * 
 * @date 2021年3月9日
 * 
 */
@RestController
@Api(value = "订单接口", description = "订单控制器，实现了订单的检索，增加，修改和删除等操作")
public class OrderController {

	Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	private OrdersService orderService;

	/**
	 * 
	 * 
	 * <p>
	 * Title: listOrders
	 * </p>
	 * 
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("api/orders")
	@ApiOperation(value = "订单检索", notes = "实现模糊查询加分页", httpMethod = "GET", response = AjaxResponse.class)
	public AjaxResponse<Order> listOrders(
			@ApiParam(name = "keywords", required = false, value = "订单编号查询关键字") @RequestParam(value = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page,
			@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
			@RequestParam(value = "begindate", required = false, defaultValue = "") String begindate,
			@RequestParam(value = "enddate", required = false, defaultValue = "") String enddate) {

		// 调用业务层方法实现分页查询
		PageInfo<Order> pageinfo = orderService.getByPage(keywords, begindate, enddate, page, limit);
		// 将返回结果转换为视图层能接受的数据格式，AjaxResponse=>Layui所以自己封装了一个对象
		AjaxResponse<Order> ar = new AjaxResponse<Order>(0, "success", (int) pageinfo.getTotal(), pageinfo.getList());

		return ar;
	}

	/**
	 * 
	
	* <p>Title: initOrder</p>
	
	* <p>Description:  购物车信息接收，存储到session ==>用来下单 ，和 支付 </p>
	
	* @param shopcObjs
	* @param session
	* @return
	 */

	@PostMapping("api/initorders.do")
	@ResponseBody
	public AjaxResponse initOrder(@RequestBody ShopcObjs shopcObjs, HttpSession session) {
		// 对象集合 --> 字符串

		System.out.println("shopcObjs : " + shopcObjs.getShopcObjs());
		session.setAttribute("list", shopcObjs.getShopcObjs());
		// 计算当前订单的总金额
		double ztotal = 0;
		for (ShopcObj shopcObj : shopcObjs.getShopcObjs()) {
			ztotal += shopcObj.getTotal();
		}
		// 总金额 存储到session
		session.setAttribute("ztotal", ztotal);

		AjaxResponse ar = new AjaxResponse();

		if (shopcObjs.getShopcObjs().size() != 0) {
			ar.setMsg("操作成功!");
			ar.setCode(0);
		} else {
			ar.setMsg("操作失败!");
			ar.setCode(1);
		}
		return ar;
	}

	/**
	 * 
	 * 
	 * <p>
	 * Title: addOrder
	 * </p>
	 * 
	 * <p>
	 * Description:// session读数据  到 订单 ==> 支付
	 * </p>
	 * 
	 * @param shopcObjs
	 * @param session
	 * @return
	 */
	@PostMapping(value = "api/order.do")
	public AjaxResponse addOrder(HttpSession session) {

		List<ShopcObj> list = (List<ShopcObj>) session.getAttribute("list");
		// 登录信息在session
		User user = (User) session.getAttribute("user");

		// 调用工具类的orderid
		String orderid = OrderIdUtil.getOrderId();

		// 订单总 金额
		double ztotal = (double) session.getAttribute("ztotal");
		System.out.println("ztotal : " + ztotal);

		// 当前订单的 商品总数量
		int ordersum = 0;

		List<Items> itemsList = new ArrayList<Items>();
		// 得到shopcid
		List<Integer> shopcids = new ArrayList();
		// 订单项目
		Items item = null;
		for (ShopcObj shopcObj : list) {
			ordersum += shopcObj.getGoodscount();
			//
			int count = shopcObj.getGoodscount();
			int goodsid = shopcObj.getGoodsid();
			int shopid = shopcObj.getShopid();
			int shopcid = shopcObj.getShopcid();
			double cost = shopcObj.getTotal();
			// new Items(count, goodsid, orderid, cost, userid, shopid, shopcid)
			item = new Items(count, goodsid, orderid, cost, user.getUserid(), shopid, shopcid);
			itemsList.add(item);

			// 得到购物车中 对应每一条记录的编号
			shopcids.add(shopcid);
		}

		Order order = new Order();

		order.setOrderdate(new Date());// 当前时间
		order.setOrderstate(0);// 固定，未支付状态
		order.setOrdersum(ordersum);// 当前订单的 商品总数量
		order.setTotal(ztotal);// 当前订单的 总金额
		order.setUserid(user.getUserid());// 下单用户的编号

		order.setOrderid(orderid);

		// 设置Items项目
		order.setItems(itemsList);

		// 购物车shopcid
		order.setShopcids(shopcids);
		// 需要提供给service对象 数据:
		logger.info("order :" + order);
		logger.info("itemsList :" + itemsList);
		logger.info("shopcids :" + shopcids);

		boolean flag = orderService.addOrder(order);

		// order存入session
		session.setAttribute("orderid", orderid);
		
		AjaxResponse ar = new AjaxResponse<>();
		if (flag) {
			ar.setCode(200);
			ar.setMsg("success");
		} else {
			ar.setCode(0);
			ar.setMsg("error");
		}

		return ar;

	}

}
